#!/usr/bin/env python
"""
AI评论审核系统依赖安装脚本
用于安装本地Qwen模型和百度API所需的依赖包
"""

import subprocess
import sys
import os

def install_package(package):
    """安装Python包"""
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        print(f"✅ {package} 安装成功")
        return True
    except subprocess.CalledProcessError:
        print(f"❌ {package} 安装失败")
        return False

def main():
    """主安装函数"""
    print("🚀 开始安装AI评论审核系统依赖...")
    print("=" * 50)
    
    # 基础依赖包
    base_packages = [
        "pymongo",           # MongoDB连接
        "redis",             # Redis连接
        "requests",          # HTTP请求
        "langchain",         # LangChain框架
        "transformers",      # HuggingFace模型
        "torch",             # PyTorch
        "accelerate",        # 模型加速
        "sentencepiece",     # 分词器
        "protobuf",         # 协议缓冲区
    ]
    
    print("📦 安装基础依赖包...")
    for package in base_packages:
        install_package(package)
    
    print("\n" + "=" * 50)
    print("🔧 安装本地Qwen模型相关依赖...")
    
    # Qwen模型相关依赖
    qwen_packages = [
        "transformers>=4.35.0",
        "torch>=2.0.0",
        "accelerate>=0.20.0",
        "sentencepiece>=0.1.99",
        "protobuf>=3.20.0",
    ]
    
    for package in qwen_packages:
        install_package(package)
    
    print("\n" + "=" * 50)
    print("🔑 百度API相关依赖...")
    
    # 百度API相关依赖
    baidu_packages = [
        "requests>=2.25.0",
        "urllib3>=1.26.0",
    ]
    
    for package in baidu_packages:
        install_package(package)
    
    print("\n" + "=" * 50)
    print("✅ 依赖安装完成！")
    print("\n📋 下一步操作：")
    print("1. 确保本地Qwen模型路径正确：D:\\PY\\Model\\Qwen-1_8B-Chat")
    print("2. 配置百度API密钥（在settings/dev.py中）")
    print("3. 运行数据库迁移：python manage.py migrate")
    print("4. 启动AI审核工作器：python manage.py start_audit_worker")
    print("5. 测试系统：python test_comment_system.py")

if __name__ == "__main__":
    main() 